﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;

namespace Paradoxlost.WebParts
{
	partial class ExchangeCalendarWebPart
	{
		private void RegisterScripts()
		{
			ClientScriptManager manager = Page.ClientScript;

			// register the base scripts
			manager.RegisterClientScriptResource(this.GetType(), "Paradoxlost.WebParts.Resources.jquery.js");
			manager.RegisterClientScriptResource(this.GetType(), "Paradoxlost.WebParts.Resources.fullcalendar.min.js");
			manager.RegisterClientScriptResource(this.GetType(), "Paradoxlost.WebParts.Resources.jquery.simplemodal-1.3.4.min.js");
			manager.RegisterClientScriptResource(this.GetType(), "Paradoxlost.WebParts.Resources.ServiceProxy.js");
			manager.RegisterClientScriptResource(this.GetType(), "Paradoxlost.WebParts.Resources.micro-template.js");
			manager.RegisterClientScriptResource(this.GetType(), "Paradoxlost.WebParts.Resources.excal.js");

			// register the details template
			if (!manager.IsClientScriptBlockRegistered("excal_layout"))
			{
				System.IO.Stream resource = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("Paradoxlost.WebParts.Resources.event-detail.html");
				System.IO.StreamReader reader = new System.IO.StreamReader(resource);
				manager.RegisterClientScriptBlock(this.GetType(), "excal_layout",
					"\r\n<script type=\"text/html\" id=\"excal_details\">\r\n" +
					reader.ReadToEnd() +
					"\r\n</script>\r\n",
					false);
				reader.Dispose();
				resource.Dispose();
			}

			// register our variables
			if (!manager.IsClientScriptBlockRegistered("excal_var"))
			{
				manager.RegisterClientScriptBlock(this.GetType(), "excal_var",
					string.Format(
						"var serviceUrl = '{0}';\r\nvar calendarId = '{1}';\r\n",
						SPUrlUtility.CombineUrl(SPContext.Current.Web.Url, "_layouts/ExCal/Calendar.asmx/"),
						""),
					true);
			}

			// register the startup script
			if (!manager.IsClientScriptBlockRegistered("excal_init"))
			{
				manager.RegisterClientScriptBlock(
					this.GetType(),
					"excal_init",
					string.Format("\r\ninitCalendar('#{0}');\r\n", this.calendarDiv.ClientID),
					true);
			}
		}
	}
}
